<?php

namespace iBrand\Component\Purchase\Repositories\Eloquent;

use iBrand\Component\Purchase\Models\TakeStock;
use iBrand\Component\Purchase\Repositories\TakeStockRepository;
use Prettus\Repository\Eloquent\BaseRepository;

class TakeStockRepositoryEloquent extends BaseRepository implements TakeStockRepository
{
	public function model()
	{
		return TakeStock::class;
	}

	public function getTakeStockPaginate($where = [], $time = [], $limit = 10)
	{
		$query = $this->scopeQuery(function ($query) use ($where, $time) {
			$query = $query->Where(function ($query) use ($where, $time) {
				if (count($where)) {
					foreach ($where as $key => $value) {
						if (is_array($value)) {
							list($operate, $va) = $value;
							$query = $query->where($key, $operate, $va);
						} else {
							$query = $query->where($key, $value);
						}
					}
				}

				if (count($time)) {
					foreach ($time as $key => $value) {
						if (is_array($value)) {
							list($operate, $va) = $value;
							$query = $query->where($key, $operate, $va);
						} else {
							$query = $query->where($key, $value);
						}
					}
				}
			});

			return $query->orderBy('created_at', 'desc');
		});

		if ($limit) {
			return $query->paginate($limit);
		} else {
			return $query->all();
		}
	}
}